Inheritance is one of the important properties of Object-Oriented Programming, Inheritance allows us to inherit the properties of one class to another. Inheritance increases the concept of code-reusability, which means we need to write code once and we can use it again and again in different places of the program.
Inheritance concepts
When we implement the concept of inheritance in C++, we often use the terms like Base and Derived Class. Base Class: The class which properties will be inherited by Derived class; it is also known as Superclass. Derived Class: The class will inherit the property of Base Class. Let’s understand Inheritance with a real-world example, consider there is an automobile company and they create cars and trucks both car and truck are vehicles which mean their properties should be matched to the common vehicle properties, so when we create a car or truck, we inherit the properties of the vehicle. Similarly, in programming, we could require a class that shares some properties similar to the other class so instead of writing the code, again and again, we simply inherit another class properties in our new class, and this will save our time and reduce the program code.
Need For inheritance
- One of the main reasons for Inheritance to show the relation between models, if they shear some same properties.
- Code reusability is also a major feature of inheritance.
Inheritance Syntax
class Base_Class_name { // base class properties }; class Deriverd_Class_Name :visibility_mode Base_Class_Name { //derived class properties };
Visibility Mode in Inheritance(access modifiers)
There are three visibility modes private, public and protected, and the visibility mode defines which properties of the Base class can be inherited by the Derived class. Visibility modes are also known as access modifiers Visibility modes are used by the derived class.
- public visibility mode
- private visibility mode
- protected visibility mode.
public visibility mode
If the derived class use the public visibility mode, then the drive class will be able to access the public and protected members of the base class. Using the public visibility mode, the public members of base class become the public members of derived class and the protected member of base class become the protected member of derived class. Example:
class Base { private: int a; public: int b; protected: int z; }; class Derived : public Base // derived class approach base class via public visibility mode { private: int x; public: int y; protected: int z; };
private visibility mode
In private visibility mode, the derived class can access the public and protected properties of base class privately, which mean all the public and protected members of the base class become the private members of the derived class. Example
class Derived : private Base // derived class approach base class via private visibility mode
protected visibility mode
In protected visibility mode, the derived class can access the public and protected members of the base class, which mean all the public and protected members of the base class become the protected members of the derived class.
class Derived : protected Base // derived class approach base class via protected visibility mode
visibility-modes
Visibility Mode (Access Modifiers) | inherited private members of base class become derived class | inherited public members of base class become derived class | inherited protected members of base class become derived class |
public | Cannot access private class | public | protected |
private | Cannot access private class | private | private |
protected | Cannot access private class | protected | protected |
Visibility Mode (Access Modifiers) | Own class accessibility | Inheritable | Accessible form object outside class |
public | Yes | yes | Yes |
private | yes | No | No |
protected | Yes | Yes | No |
Types of Inheritance
The types of inheritance define which framework is followed by the derived and base class.
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
1. Single Inheritance
In Single Inheritance, we have a total of two classes base and derived class. Here the derived class inherit directly from the base class. Example
#include <iostream> using namespace std; // base class class Mammals { public: Mammals () { cout << "Mammals give birth" << endl; } }; class Cat: public Mammals { }; // main function int main() { // creating object of derived class will // invoke the constructor of base classes Cat tom; return 0; }
Output
Mammals give birth
Behind the code In this above example, we created the object of Cat class but it invokes the constructor of Mammals class, this is because the Cat has inherited the Mammals class.
2. Multiple inheritance:
In Multiple Inheritance the there is one derived class and it inherits the properties of more than one base class.
Example:
#include <iostream> using namespace std; // base class class Mammals { public: Mammals () //constructor { cout << "Mammals give birth" << endl; } }; class Cat { public: Cat() { cout<<"It's a Cat Family"<<endl; } }; class Lion: public Mammals, public Cat //inheriting more than one base class { public: }; // main function int main() { // creating object of derived class will // invoke the constructor of base classes Lion king; return 0; }
Output
Mammals give birth It's a Cat Family
Behind the Code In this example the Lion class inherit the Mammals and Cat class, and when the object of Lion class gets created the constructor of Mammals and Cat get invoked.
3. Hierarchical inheritance
In Hierarchical Inheritance, multiple derived class inherit from the single base class. Example
#include <iostream> using namespace std; // base class class Mammals { public: Mammals () //constructor { cout << "Mammals give birth" << endl; } }; class Cat: public Mammals //inhereting Mammals Class { public: Cat() //constructor { cout<<"It's a Cat "<<endl; } }; class Dog: public Mammals //inheriting Mammals Class { public: Dog() //constructor { cout<<"It's a Dog "<<endl; } }; // main function int main() { // creating object of derived class will // invoke the constructor of base classes cout<<"-----When Create a Dog Object----\n"; Dog luffy; cout<<"-----When Create a cat Object----\n"; Cat tom; return 0; }
Output
-----When Create a Dog Object---- Mammals give birth It's a Dog -----When Create a cat Object---- Mammals give birth It's a Cat
Behind the Code In this example both Cat and Dog class inherit the Mammals class so when the object of both the classes get created Dog luffy; and Cat tom; they also invoke the Mammals Class.
4. Multilevel Inheritance
In Multilevel Inheritance, the derived class become the base class for another derived class. To form a Multilevel Inheritance, we at least have three classes.
Example:
#include <iostream> using namespace std; // base class class A { public: A () //constructor { cout << "Class A has Invoked" << endl; } }; class B: public A //inheriting A Class { public: B() //constructor { cout<<"Class B has Invoked "<<endl; } }; class C: public B //inhereting B Class { public: C() //constructor { cout<<"Class C has Invoked"<<endl; } }; // main function int main() { // creating object of derived class C c; return 0; }
Output
Class A has Invoked Class B has Invoked Class C has Invoked
Behind the Code In this example, we created the object of Class C and first it invoked class B and class B invoked class A, that’s why the Output of Class A show first, then Class B and at last class C.
5. Hybrid Inheritance
If two or more type of Inheritance type mixed together then it forms a Hybrid Inheritance. Example
#include <iostream> using namespace std; // base class class A { public: A () { cout << "Class A has Invoked" << endl; } }; class B { public: B() { cout<<"Class B has Invoked "<<endl; } }; class C: public B //inheriting B Class { public: C() // constructor { cout<<"Class C has Invoked"<<endl; } }; class D: public C, public A //inhereting Class C and A { public: }; // main function int main() { // creating object of derived class D d; return 0; }
Output
Class B has Invoked Class C has Invoked Class A has Invoked
Inheritance Quick Summary
- Inheritance is one of the most important properties of Object-Oriented Programming.
- With inheritance, w can inherit the public and protected properties of one class to another.
- The visibility mode or access modifiers define where would be the public and protected members of a base class fall inside the Derived class.
- Private members of the class can only access the class.
- There are three types of Access modifiers Private, Protected and Public.
- Inheritance is divided into 5 types Single, Multiple, Hierarchical, Multilevel and hybrid.
- Hybrid is a collection of two or more than two inheritances.
People are also reading: